import { ApiHideProperty } from "@nestjs/swagger";
import { Type } from "class-transformer";
import { IsNumber, IsOptional, IsString, isNumber } from "class-validator";
import {
  Column,
  Entity,
  ManyToMany,
  PrimaryGeneratedColumn,
  Tree,
  TreeChildren,
  TreeParent,
} from "typeorm";
import { Role } from "../../role/entities/role.entity";
import { BaseEntity } from "src/common/entities/base.entity";

@Entity()
@Tree("materialized-path")
export class Menu extends BaseEntity {
  @PrimaryGeneratedColumn({
    name: "menu_id",
    comment: "菜单id",
    type: "int",
  })
  @Type()
  @IsNumber()
  menu_id: number;

  @Column({
    name: "name",
    comment: "路由别名",
    length: 200,
  })
  @IsString()
  @IsOptional()
  name?: string;

  @Column({
    name: "menu_name",
    comment: "菜单名称",
    length: 50,
  })
  @IsString()
  menu_name: string;

  @Column({
    name: "order_num",
    comment: "显示顺序",
  })
  @IsNumber()
  order_num: number;

  @Column({
    name: "path",
    comment: "路由地址",
    length: 200,
    default: "",
  })
  @IsOptional()
  @IsString()
  path: string;

  @Column({
    name: "component",
    comment: "组件路径",
    length: 255,
    default: null,
  })
  @IsOptional()
  @IsString()
  component?: string;

  @Column({
    name: "query",
    comment: "路由参数",
    length: 255,
    default: null,
  })
  @IsOptional()
  @IsString()
  query?: string;

  @Column({
    name: "is_frame",
    comment: "是否为外链（0否 1是）",
    type: "int",
    default: 0,
  })
  @IsOptional()
  @Type()
  @IsNumber()
  is_frame: number;

  @Column({
    name: "link_url",
    comment: "外链地址",
    length: 255,
    default: null,
  })
  @IsOptional()
  @IsString()
  link_url?: string;

  @Column({
    name: "is_keepAlive",
    comment: "是否缓存（0否 1是）",
    type: "int",
    default: 0,
  })
  @IsOptional()
  @Type()
  @IsNumber()
  is_keepAlive: number;

  /* '菜单类型 */
  // @Column({
  //   name: "menu_type",
  //   comment: "菜单类型（M目录 C菜单 F按钮）",
  //   length: 1,
  //   type: "char",
  //   default: "C",
  // })
  // @IsString()
  // @IsOptional()
  // menu_type?: string;

  /* 菜单状态(0显示 1隐藏) */
  @Column({
    name: "is_hide",
    comment: "菜单状态（0显示 1隐藏）",
    type: "int",
    default: 0,
  })
  @IsOptional()
  @IsNumber()
  is_hide: number;

  @Column({
    name: "is_full",
    comment: "是否全屏（0否 1是）",
    type: "int",
    default: 0,
  })
  @IsOptional()
  @Type()
  @IsNumber()
  is_full: number;

  /* 菜单图标 */
  @Column({
    name: "icon",
    comment: "菜单图标",
    length: 100,
    type: "char",
    default: "#",
  })
  @IsOptional()
  @IsString()
  icon?: string;

  @ApiHideProperty()
  @TreeChildren()
  children: Menu[];

  @ApiHideProperty()
  @TreeParent()
  parent: Menu;

  @ApiHideProperty()
  @ManyToMany(() => Role, (role) => role.menus)
  roles: Role[];
}
